#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <assert.h>
#include <stdlib.h>

#define NUM 10

typedef void (*func_t)();// 函数指针

func_t handlerTask[NUM];

void task1()
{
    printf("handler task1\n");
}

void task2()
{
    printf("handler task2\n");
}

void task3()
{
    printf("handler task3\n");
}

void loadTask()
{
    memset(handlerTask, 0, sizeof(handlerTask));
    handlerTask[0] = task1;
    handlerTask[1] = task2;
    handlerTask[2] = task3;
}

int main()
{
    pid_t id = fork();
    assert(id != -1);
    if(id == 0)
    {
        //child
        int cnt = 5;
        while(cnt)
        {
            printf("child running, pid: %d, ppid: %d, cnt: %d\n", getpid(), getppid(), cnt--);
            sleep(1);
        }

        exit(0);
    }

    loadTask();
    //parent
    int status = 0;
    while(1)
    {
        pid_t ret = waitpid(id, &status, WNOHANG);//非阻塞->子进程没有退出,父进程检测之后立即返回
        if(ret == 0)
        {
            //waitpid调用成功 && 子进程没有退出
            //子进程没有退出, waitpid没有等待失败,仅仅是检测到了子进程没有退出
            printf("wait done, but child is running..., parent running other things\n");
            for(int i = 0; handlerTask[i] != NULL; i++)
                handlerTask[i]();
        }
        else if(ret > 0)
        {
            //waitpid调用成功 && 子进程退出
            printf("wait success, exit code: %d, sig: %d\n", (status>>8)&0xFF, status & 0x7F);
            break;
        }
        else
        {
            //waitpid调用失败
            printf("waitpid call failed\n");
            break;
        }
        sleep(1);
    }
}